import {canReset, resetDefault} from '$common/admin/utils';
import MobxStore from '$common/mobxStore'
import {observable, action, runInAction, computed, toJS,flow} from 'mobx'
import {UserApi} from '$api/index'
import {IS_OK} from '$config/admin/state_code'
import {appendKeyToArr} from '$common/admin/utils';

class Model extends MobxStore {
  @observable @resetDefault(false) loading = false;
  @observable @resetDefault([])  tableData = [];
  @observable @resetDefault(1) tablePage = 1;
  @observable @resetDefault(0) listTotal = 0;
  @observable @resetDefault(8) table_page_size = 8;

  @action
  toggleLoading = (state = !this.loading) => {
    this.loading = state;
  };

  @action
  setPageSize = (value) => {
    this.table_page_size = value;
  };

  @action
  setTablePage = (value)=>{
    this.tablePage = value;
  };

  @action
  setListTotal = (value)=>{
    this.listTotal = value;
  };

  delUser = async (id)=>{
    let Rs = await UserApi.delUser({id});
    return Rs
  };

  eidtPassWord = async ({id,password})=>{
    let Rs = await UserApi.editUserPassWord({id,password});
    return Rs;
  };

  formatTableData = (data)=>{
    data = appendKeyToArr(data);
    _.each(data,item=>{
        item.inputValue = "";
    });
    return data;
  };

  @action
  editItemInputValue = (val,id)=>{
      let findItem = _.find(this.tableData,{id});
      findItem.inputValue = val;
  };

  getUserList = flow(function *(page = 1, filter = undefined, size = this.table_page_size) {
    try {
      let Rs = yield UserApi.getNormalUsers({page, filter, size});
      if (Rs.data.error_code === IS_OK) {
        let {total, list, page} = Rs.data.data;
        this.tableData = this.formatTableData(list);
        this.listTotal = total;
        this.tablePage = page;
      }
      return Rs
    } catch (e) {
      console.log(e)
    }
  })
}

export default Model
